package com.fitbet.wechat.web.controller.main;

import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.core.wechat.entity.cfg.JsConfig;
import com.fitbet.entity.bet.UserBet;
import com.fitbet.entity.bet.UserBetStatus;
import com.fitbet.entity.user.User;
import com.fitbet.wechat.processor.WechatProcessor;
import com.fitbet.wechat.service.UserBetService;
import com.fitbet.wechat.web.resolver.Secure;

/**
 * 
 * @author mohanlan
 *
 */
@Controller
@RequestMapping("/profile/bets")
public class BetVedioController {
    
    private static final Logger logger = LoggerFactory.getLogger(BetVedioController.class);
    
    @Value("#{cloud['qiniu.host']}")
    private String qiniuHost;
    
    @Autowired
    private WechatProcessor wechatProcessor;
    
    
    @Autowired
    private UserBetService userBetService;
    
    @RequestMapping(value = "/{betId}/vedio-upload", method = RequestMethod.GET)
    public ModelAndView openVedio(@Secure User user,
                                  @PathVariable("betId") Long betId,
                                  @RequestParam("phase") String phase,
                                  HttpServletRequest request) {
        
        ModelAndView modelAndView = new ModelAndView("profile/bet-vedio-upload");
        modelAndView.addObject("user", user);
        
        UserBet userBet = userBetService.loadUserBet(user.getId(), betId, new UserBetStatus[]{UserBetStatus.PAID, UserBetStatus.PROCESSING, UserBetStatus.CLOSEFAILED, UserBetStatus.OPEN, UserBetStatus.OPENFAILED, UserBetStatus.CLOSE});
        modelAndView.addObject("userBet", userBet);
        
        String curl = WebUtils.getRealRequestURL(request);
        JsConfig jsConfig = wechatProcessor.generateJsConfig(curl);
        modelAndView.addObject("jsConfig", JsonUtils.toJson(jsConfig));
        
        return modelAndView;
    }
    
    /**
     * 
     * @param user
     * @param betId
     * @param phase
     * @param mediaId
     * @param request
     * @return
     */
    @RequestMapping(value = "/{betId}/vedio-upload", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onVedioUpload(@Secure User user,
                                    @PathVariable("betId") Long betId,
                                    @RequestParam("phase") String phase,
                                    @RequestParam("status") String status,
                                    @RequestParam("videoPath") String videoPath,
                                    HttpServletRequest request) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            userBetService.uploadVideo(user.getId(), betId, qiniuHost + "/" + videoPath, phase, status);
            httpResult.success().message("上传视频成功");
            
        } catch (Exception e) {
            logger.error("onVedioUpload occur error", e);
            httpResult.fail("500").message("上传视频失败");
        }
        
        return httpResult;
    }

}
